package chenji.yourticket.model;

import java.util.HashMap;
import java.util.TreeSet;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.inventory.ItemStack;

import chenji.yourticket.Main;
import chenji.yourticket.datacenter.PlayerData;
import chenji.yourticket.inventory.SimpleInventory;
import chenji.yourticket.inventory.SimpleItemStack;

public class Model extends SimpleInventory {
	private boolean full;
	private Player p;
	private TreeSet<ModelInfo> infoList = new TreeSet<ModelInfo>();
	private HashMap<Integer, ModelInfo> cache = new HashMap<Integer, ModelInfo>();
	int maxPage;
	int nowPage = 0;

	public Model(Player p, TreeSet<ModelInfo> infoList) {
		super(p, (short) 7, Main.getMessageManager().getModel_Title().replaceAll("%player%", p.getName()), 6);

		this.infoList = infoList;
		this.p = p;
		this.maxPage = this.infoList.size() / 28 + 1;
		refuseChengePageItem();
		installItemStack();
		this.chengePage(0);
	}

	private void refuseChengePageItem() {
		ItemStack upPage = new SimpleItemStack(Material.NETHER_STAR,
				Main.getMessageManager().getItem_UpPage_Lore()
						.replaceAll("%nowPage%", Integer.toString(this.nowPage + 1)).replace("%maxpage%",
								Integer.toString(this.maxPage)),
				Main.getMessageManager().getItem_UpPage_Title());
		ItemStack nextPage = new SimpleItemStack(Material.NETHER_STAR,
				Main.getMessageManager().getItem_NextPage_Lore()
						.replaceAll("%nowPage%", Integer.toString(this.nowPage + 1)).replace("%maxpage%",
								Integer.toString(this.maxPage)),
				Main.getMessageManager().getItem_NextPage_Title());
		this.getInventory().setItem(46, upPage);
		this.getInventory().setItem(52, nextPage);

	}

	private void upPage() {
		this.chengePage(this.nowPage - 1);
	}

	private void nextPage() {
		this.chengePage(this.nowPage + 1);
	}

	private void chengePage(int page) {
		if (this.maxPage <= page || page < 0) {
			return;
		}
		this.nowPage = page;
		this.clear();
		this.cache.clear();
		int i = 0;
		int start = 28 * page;
		int ca = 0;
		for (ModelInfo info : this.infoList) {
			i++;
			if (i >= start && i <= start + 28) {
				ca++;
				cache.put(ca, info);
				this.getInventory().addItem(info.getDisplayItemStack());
			}
		}
		refuseChengePageItem();
	}

	public void clear() {
		int[] i = new int[] { 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37,
				38, 39, 40, 41, 42, 43 };
		for (int ia : i) {
			this.getInventory().setItem(ia, null);
		}
	}

	@Override
	public void installItemStack() {
		this.getInventory().setItem(51, new SimpleItemStack(Material.ENCHANTED_BOOK,
				Main.getMessageManager().getItem_AddModel_Lore(), Main.getMessageManager().getItem_AddModel_Title()));
		int nowAmount = Main.getPlayerDataChenter().getPlayerData(p).getAmount();
		int maxAmount = Main.getLimitManager().getLimit(p);
		if (nowAmount >= maxAmount) {
			this.getInventory().setItem(51,
					new SimpleItemStack(Material.STAINED_GLASS_PANE,
							Main.getMessageManager().getItem_CannotCreate_Lore(),
							Main.getMessageManager().getItem_CannotCreate_Title(), (short) 14));
			full = true;
		} else {
			getInventory().setItem(51,
					new SimpleItemStack(Material.ENCHANTED_BOOK, Main.getMessageManager().getItem_AddModel_Lore(),
							Main.getMessageManager().getItem_AddModel_Title()));
			full = false;
		}
		this.getInventory().setItem(47, new SimpleItemStack(Material.ENCHANTED_BOOK,
				Main.getMessageManager().getItem_Info_Lore().replaceAll("%nowamount%", Integer.toString(nowAmount))
						.replaceAll("%maxamount%", Integer.toString(maxAmount))
						.replace("%amount%", Integer.toString(maxAmount - nowAmount)),
				Main.getMessageManager().getItem_Info_Title()));
	}

	public void ClickItem(int slot, InventoryAction I) {
		if (slot == 46) {
			this.upPage();
			return;
		}
		if (slot == 52) {
			this.nextPage();
			return;
		}
		if (slot == 51) {
			if (full) {
				return;
			}
			this.p.closeInventory();
			Main.getEditorManager().newModelInfo(p);
			return;
		}
		int l = 0;
		if (slot >= 10 && slot <= 16) {
			l = slot - 9;
		} else if (slot >= 19 && slot <= 25) {
			l = slot - 11;
		} else if (slot >= 27 && slot <= 33) {
			l = slot - 13;
		} else if (slot >= 35 && slot <= 42) {
			l = slot - 15;
		} else {
			return;
		}
		if (I.equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
			ModelInfo i = this.cache.get(l);
			PlayerData pd = Main.getPlayerDataChenter().getPlayerData(p);
			if (pd == null) {
				p.sendMessage(Main.getMessageManager().getMessage_UnkonwException());
				return;
			}
			pd.removeModel(i.getName());
			Main.getModelManager().reloadInventory(p);
			return;
		} else {

			ModelInfo i = this.cache.get(l);
			if (i == null) {
				return;
			}
			Main.getModelManager().openActionChooseInventory(p, i);
			return;
		}

	}

	public ModelInfo getModel(int solt) {
		int number = 36 * this.nowPage + solt;
		int now = 0;
		for (ModelInfo m : this.infoList) {
			now++;
			if (now == number) {
				return m;
			}
		}
		return null;
	}

}
